#include <tool.h>

double BreakOut(DATA data[], int datanum, int index, int range, BreakOutType type)
{
	double d1 = (type==BREAKOUT_LOW)?9999:0;

	range -= 1;
	while(range >= 0)
	{
		if( data[index+range].close > 0 )
		{
			if(type==BREAKOUT_LOW)
				d1 = (data[index+range].close<d1)?(data[index+range].close):(d1);
			else if(type==BREAKOUT_HIGH)
				d1 = (data[index+range].close>d1)?(data[index+range].close):(d1);
		}
		else
			return 0;
		range -= 1;
	}
	return d1;
}

double TureRange(DATA data[], int datanum, int index)
{
	double d1, d2, d3, d4;

	if( data[index].close > 0 && data[index+1].close > 0 )
	{
		d1 = (data[index].high > data[index].low)?(data[index].high - data[index].low):(data[index].low - data[index].high);
		d2 = (data[index].high > data[index+1].close)?(data[index].high - data[index+1].close):(data[index+1].close - data[index].high);
		d3 = (data[index].low > data[index+1].close)?(data[index].low - data[index+1].close):(data[index+1].close - data[index].low);
		d4 = (d1>d2)?d1:d2;
		d4 = (d4>d3)?d4:d3;
		return d4;
	}
	else
		return 0;
}

double AverageTrueRange(DATA data[], int datanum, int index, int range)
{
	int i;
	double d1;

	if( data[index+1].atr == 0 ) // Average
	{
		d1 = 0;
		for(i=0; i<range; i++)
		{
			if( data[index+i].tr > 0 )
				d1 += data[index+i].tr;
			else
				return 0;
		}
		d1 /= range;
		return d1;
	}
	else
		return ( data[index+1].atr * (range-1) + data[index].tr ) / range ;
}

